package com.service.impl;

import com.github.pagehelper.PageHelper;
import com.mapper.GroupListMapper;
import com.pojo.GroupList;
import com.pojo.GroupListExample;
import com.pojoVO.GroupListVO;
import com.respository.GroupListRepository;
import com.service.GroupListService;
import com.util.MessageUtil;
import com.util.QiniuConfig;
import com.util.ServerResponse;
import com.util.UploadImgUtil;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2020/3/19 0019.
 */

@Service
public class GroupListServiceImpl implements GroupListService {
    @Autowired
    GroupListMapper groupListMapper;
    @Autowired
    GroupListRepository groupListRepository;
    @Override
    public ServerResponse save(GroupList groups) {
        Byte status =1;
        groups.setStatus(status);
        //执行mybatis添加方法
        int rows = groupListMapper.insert(groups);
        //判断添加成功
        if (rows > 0)
            return ServerResponse.addSuccess();
        else
            return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse modifyStatus(Integer id) {
        GroupList groups = groupListMapper.selectByPrimaryKey(id);
        Byte status = 0;
        groups.setStatus(status);
        int i = groupListMapper.updateByPrimaryKeySelective(groups);
        if (i>0)
            return ServerResponse.deleteSuccess();
        return ServerResponse.deleteFailed();
    }

    @Override
    public ServerResponse modify(GroupList groups) {
        int i = groupListMapper.updateByPrimaryKeySelective(groups);
        if (i>0)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }

    @Override
    public ServerResponse getById(Integer id) {
        GroupList groups = groupListMapper.selectByPrimaryKey(id);
        return ServerResponse.getDataSuccess(groups);
    }

    @Override
    public ServerResponse getAll() {
        GroupListExample example=new GroupListExample();
        GroupListExample.Criteria criteria = example.createCriteria();
        Byte status = 1;
        criteria.andStatusEqualTo(status);
        List<GroupList> groupsList= groupListMapper.selectByExample(example);
        if (groupsList!=null&&groupsList.size()>0)
            return ServerResponse.getDataSuccess(groupsList);
        return ServerResponse.getDataFailed("没有数据");
    }
    @Override
    public ServerResponse sale(String type){
        GroupListExample example=new GroupListExample();
        GroupListExample.Criteria criteria = example.createCriteria();
        criteria.andGroupTypeLike("%" + type + "%");
        Byte status = 1;
        criteria.andStatusEqualTo(status);
        example.setOrderByClause("createtime desc");
        PageHelper.startPage(1,3);
        List<GroupList> groupsList = groupListMapper.selectByExample(example);
        if (groupsList!=null&&groupsList.size()>0){
            GroupListVO groupListVO = new GroupListVO();
            groupListVO.setGroupKeyword(groupsList.get(0).getGroupKeyword());
            groupListVO.setGroupEndtime(groupsList.get(0).getGroupEndtime());
            groupListVO.setGroupType(groupsList.get(0).getGroupType());
            groupListVO.setGroupDatalist(groupsList);
            ServerResponse serverResponse = ServerResponse.getDataSuccess(groupListVO);
            return serverResponse;
        }else {
            return ServerResponse.getDataFailed(MessageUtil.NO_DATA);
        }
    }
    @Override
    public String getQiniuToken() {
        String upToken = UploadImgUtil.getUpToken();
        return upToken;
    }


    @Override
    public GroupList getOrderById(Integer id) {
        return  groupListMapper.selectByPrimaryKey(id);
    }

    @Override
    public ServerResponse getAllBySearch(String groupKeyword, String groupFuzzy, String other1, String groupPrice) {
        System.out.printf(groupKeyword);
        System.out.printf(groupFuzzy);
        System.out.printf(other1);
        System.out.printf(groupPrice);
        GroupListExample example=new GroupListExample();
        GroupListExample.Criteria criteria = example.createCriteria();
        Byte status = 1;
        String [] num = new String[2];
        num = other1.split("-");
        Integer minNum = Integer.parseInt(num[0]);
        System.out.println(minNum);
        Integer maxNum = Integer.parseInt(num[1]);
        System.out.println(maxNum);
        String [] price = groupPrice.split("-");
        Float minPrice  = Float.parseFloat(price[0]);
        System.out.println(minPrice);
        Float maxPrice = Float.parseFloat(price[1]);
        System.out.println(maxPrice);
        System.out.println(groupKeyword);
        System.out.println(groupFuzzy);
        if(groupKeyword.equals("不限")&&groupFuzzy.equals("不限")) {
            System.out.println(11);
            criteria.andStatusEqualTo(status).andGroupPriceLessThanOrEqualTo(maxPrice)
                    .andGroupPriceGreaterThanOrEqualTo(minPrice);
        }
        else if(groupKeyword.equals("不限")&&!groupFuzzy.equals("不限")){

            criteria.andStatusEqualTo(status).andOther1LessThanOrEqualTo(maxNum).andOther1GreaterThanOrEqualTo(minNum).andGroupPriceLessThanOrEqualTo(maxPrice).andGroupPriceGreaterThanOrEqualTo(minPrice).andGroupFuzzyLike("%"+groupFuzzy+"%");
            System.out.println(22);
        }
        else if(groupFuzzy.equals("不限")&&!groupKeyword.equals("不限")){
            criteria.andStatusEqualTo(status).andOther1LessThanOrEqualTo(maxNum).andOther1GreaterThanOrEqualTo(minNum).andGroupPriceLessThanOrEqualTo(maxPrice).andGroupPriceGreaterThanOrEqualTo(minPrice).andGroupKeywordLike("%"+groupKeyword+"%");
            System.out.println(33);
        }else if(!groupFuzzy.equals("不限")&&!groupKeyword.equals("不限")) {
            criteria.andStatusEqualTo(status).andOther1LessThanOrEqualTo(maxNum).andOther1GreaterThanOrEqualTo(minNum).andGroupPriceLessThanOrEqualTo(maxPrice).andGroupPriceGreaterThanOrEqualTo(minPrice).andGroupKeywordLike("%"+groupKeyword+"%").andGroupFuzzyLike("%"+groupFuzzy+"%");
            System.out.println(44);
        }

        List<GroupList> groupsList= groupListMapper.selectByExample(example);
        System.out.println(groupsList);
        if (groupsList!=null&&groupsList.size()>0)
            return ServerResponse.getDataSuccess(groupsList);
        return ServerResponse.getDataFailed("没有数据");
    }
    @Override
    public ServerResponse getBySearch(String kw) {
        System.out.println("service kw:"+kw);
        //构建多条件查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (kw != null && !"".equals(kw.trim())){
            System.out.println("1111");
            //定义需要查询匹配字段
            String [] args = {"group_name","group_content","group_detail","group_inform","group_keyword","group_city","group_fuzzy","group_type"};
            //搜索框构建多字段查询条件
            MultiMatchQueryBuilder searchBuilder = QueryBuilders.multiMatchQuery(kw, args);
            System.out.println(searchBuilder);
            //合并多字段查询
            boolQueryBuilder.must(searchBuilder);
            //spring提供的一个查询条件构造器，帮助构建json格式的请求体
            NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
            //查询条件
            builder.withQuery(searchBuilder);
            //生产nativeSearchQuery
            NativeSearchQuery build = builder.build();
            System.out.println(build);
            //调用shopRepository进行查询
            Page<GroupList> groupPage = groupListRepository.search(build);
            // 总条数
            long total = groupPage.getTotalElements();
            System.out.println("total = " + total);
            for (GroupList g : groupPage) {
                System.out.println(g);
            }
            return ServerResponse.getDataSuccess(groupPage);
        }
        return ServerResponse.getDataFailed("没有对应的店铺");
    }

}
